Add support for _meta attributes in resource contents#1638
Open
calvingiles wants to merge 3 commits intomodelcontextprotocol:mainfrom
Open
Conversation
This change enables MCP servers to include metadata in resource content using the _meta field, which is part of the MCP specification. This allows servers to provide additional context about resources, such as domain information or other custom metadata. Changes: - Added meta field to ReadResourceContents helper class - Added _meta field to Resource base class with proper alias - Updated lowlevel server handler to pass through _meta when creating TextResourceContents and BlobResourceContents - Updated FastMCP server to pass resource._meta to ReadResourceContents - Added comprehensive tests for _meta support in both lowlevel and FastMCP resources The implementation maintains backward compatibility - resources without _meta continue to work as before with meta=None.
Adds tests to verify that _meta attributes are correctly serialized with the underscore prefix in JSON output, not as "meta". This ensures the implementation conforms to the MCP specification where metadata fields use the _meta key. Tests verify: - Resource serialization includes "_meta" key in JSON - Response content serialization includes "_meta" key - JSON strings contain the literal "_meta" key - No "meta" key appears in serialized output
…ontents
Use the field name 'meta' instead of the alias '_meta' when constructing
Pydantic model instances. The alias is only used for JSON serialization.
This fixes a bug where passing **{'_meta': meta} would cause Pydantic
validation errors since '_meta' is an alias, not a field name.
Contributor
|
related: #1476 |
|
Before this is merged, is there any workaround? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This change enables MCP servers to include metadata in resource content using the _meta field, which is part of the MCP specification. This allows servers to provide additional context about resources, such as domain information or other custom metadata.
Changes:
The implementation maintains backward compatibility - resources without _meta continue to work as before with meta=None.
Motivation and Context
How Has This Been Tested?
Breaking Changes
Types of changes
Checklist
Additional context